package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private Connection con = null;
    @Override
    public User sgetUserByIdByPass(String userId, String password) {
        User user = new User();
        UserDao dao = new UserDaoImpl();
        try {
            user = dao.dgetUserByIdByPass(userId,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int sgetUserById(String userid) {
        int isRight = 0;
        UserDao dao = new UserDaoImpl();
        try {
            isRight = dao.dgetUserById(userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight;
    }


    @Override
    public int ssaveUser(User user) {
        int isRight = 0;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.beginTransaction();
            isRight = dao.dsaveUser(user);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }
}
